package bigdata36.damin;

import java.io.IOException;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Admin;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.junit.Test;

/**
 * hbase的表的增删
 * 
 * @author root
 *
 */
public class TableAdmin {

	/**
	 * 创建表
	 * 
	 * @throws Exception
	 */
	@Test
	public void testCreateTable() throws Exception {
		// hadoop里面的conf，会加载 hdfs-site.xml core-site.xml,mapred-site.xml ....
		// hadoop 里面的配置文件 ,不会加载 hbase-site.xml
		// Configuration conf = new Configuration();
		Configuration conf = HBaseConfiguration.create();// hbase-site.xml文件
		conf.set("hbase.zookeeper.quorum", "hadoop01:2181,hadoop02:2181,hadoop03:2181");
		// 获取连接
		Connection connection = ConnectionFactory.createConnection(conf);
		// 从连接中获取表管理对象
		Admin admin = connection.getAdmin();
		// 表描述
		HTableDescriptor hTableDescriptor = new HTableDescriptor(TableName.valueOf("t_user"));
		// 列簇描述
		HColumnDescriptor hColumnDescriptor1 = new HColumnDescriptor("base_info");
		hColumnDescriptor1.setMaxVersions(3);
		HColumnDescriptor hColumnDescriptor2 = new HColumnDescriptor("family");
		hTableDescriptor.addFamily(hColumnDescriptor1);
		hTableDescriptor.addFamily(hColumnDescriptor2);
		// 创建table
		admin.createTable(hTableDescriptor);
		// 关闭资源
		admin.close();
		connection.close();
	}

	/**
	 * 删除表
	 * @throws Exception 
	 */
	@Test
	public void testDropTable() throws Exception {
		// hadoop里面的conf，会加载 hdfs-site.xml core-site.xml,mapred-site.xml ....
		// hadoop 里面的配置文件 ,不会加载 hbase-site.xml
		// Configuration conf = new Configuration();
		Configuration conf = HBaseConfiguration.create();// hbase-site.xml文件
		conf.set("hbase.zookeeper.quorum", "hadoop01:2181,hadoop02:2181,hadoop03:2181");
		// 获取连接
		Connection connection = ConnectionFactory.createConnection(conf);
		// 从连接中获取表管理对象
		Admin admin = connection.getAdmin();
		
		admin.disableTable(TableName.valueOf("t_user"));
		admin.deleteTable(TableName.valueOf("t_user"));
		admin.close();
		connection.close();
	}
	
	/**
	 * list表     exists
	 */

}
